package com.example.artwindow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.artwindow.constants.HttpCodeEnum;
import com.example.artwindow.domain.po.Movie;
import com.example.artwindow.domain.dto.MovieUploadDTO;
import com.example.artwindow.domain.dto.PageMovieDTO;
import com.example.artwindow.domain.vo.MovieDetailsVO;
import com.example.artwindow.domain.vo.MovieVO;
import com.example.artwindow.exception.UnauthorizedException;
import com.example.artwindow.result.ListResult;
import com.example.artwindow.result.Result;
import com.example.artwindow.service.MovieService;
import com.example.artwindow.mapper.MovieInfoMapper;
import com.example.artwindow.tool.CopyProperties;
import com.example.artwindow.tool.QiNiuOSTool;
import com.example.artwindow.tool.TheadLocalTool;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
* @author EiEi
* @description 针对表【movie_info】的数据库操作Service实现
* @createDate 2023-12-20 12:21:12
*/
@Service
public class MovieServiceImpl extends ServiceImpl<MovieInfoMapper, Movie>
    implements MovieService {

	/**
	 * 分页查询电影信息
	 * @param pageMovieDTO
	 * @return
	 */
	@Override
	public Result<ListResult<MovieVO>> pageMovie(PageMovieDTO pageMovieDTO) {
		LambdaQueryWrapper<Movie> movieLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (!Objects.isNull(pageMovieDTO.getMovieName())){
			movieLambdaQueryWrapper.like(Movie::getMovieName,pageMovieDTO.getMovieName());
		}
		if (!Objects.isNull(pageMovieDTO.getMovieGenre())){
			movieLambdaQueryWrapper.like(Movie::getMovieGenre,pageMovieDTO.getMovieGenre());
		}
		if (!Objects.isNull(pageMovieDTO.getReleaseYear())&&pageMovieDTO.getReleaseYear()>1900){
			movieLambdaQueryWrapper.eq(Movie::getReleaseYear,pageMovieDTO.getReleaseYear());
		}
		if (!Objects.isNull(pageMovieDTO.getZone())){
			movieLambdaQueryWrapper.eq(Movie::getZone,pageMovieDTO.getZone());
		}
		movieLambdaQueryWrapper.select();
		movieLambdaQueryWrapper.orderByDesc(Movie::getCreateTime);
		Page<Movie> page = new Page<>(pageMovieDTO.getPage(), pageMovieDTO.getPageSize());
		page(page,movieLambdaQueryWrapper);
		ListResult<MovieVO> listResult = new ListResult<>(page.getTotal()
				, CopyProperties.copyProperties(page.getRecords(), MovieVO.class));
		return Result.success(listResult);
	}

	/**
	 * 更新电影信息
	 * @param movieUpLoadDTO
	 * @param movieCover
	 * @return
	 */
	@Override
	public Result<?> upLoadMovie(MovieUploadDTO movieUpLoadDTO, MultipartFile movieCover) {
		if (Objects.isNull(TheadLocalTool.getCurrentId())){
			throw new UnauthorizedException(HttpCodeEnum.ERR_NOT_LOGIN);
		}
		Long id = TheadLocalTool.getCurrentId();
		//获取认证
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		//上传其他数据
		Movie movie = BeanUtil.copyProperties(movieUpLoadDTO, Movie.class);
		//上传图片
		if (!Objects.isNull(movieCover)){
			String name1 = authentication.getName();
			String name= name1 + System.currentTimeMillis();
			String movieCoverString = QiNiuOSTool.Os(name, movieCover);
			movie.setMovieCover(movieCoverString);
		}
		save(movie);
		return Result.success();
	}

	/**
	 * 统计评论数
	 * @param movieId
	 * @param commentScore
	 */
	@Override
	public void countComment(Long movieId, Integer commentScore) {
		Movie byId = getById(movieId);
		byId.setMovieComments(byId.getMovieComments()+1);
		BigDecimal newScore = BigDecimal.valueOf(commentScore);
		BigDecimal comments = BigDecimal.valueOf(byId.getMovieComments());
		byId.setMovieScore(byId.getMovieScore().add(newScore).divide(comments, 2, RoundingMode.HALF_UP));
		updateById(byId);
	}

	/**
	 * 查询详情
	 * @param id
	 * @return
	 */
	@Override
	public Result<MovieDetailsVO> selectPlotById(Long id) {
		MovieDetailsVO movieDetailsVO = BeanUtil.copyProperties(getById(id), MovieDetailsVO.class);
		return Result.success(movieDetailsVO);
	}

	/**
	 * 查询推荐
	 * @param amount
	 * @return
	 */
	@Override
	public Result<List<MovieVO>> getMovieList(Integer amount) {
		LambdaQueryWrapper<Movie> movieLambdaQueryWrapper = new LambdaQueryWrapper<>();
		movieLambdaQueryWrapper.orderByDesc(Movie::getMovieScore);
		movieLambdaQueryWrapper.last("limit "+amount);
		List<Movie> list = list(movieLambdaQueryWrapper);
		List<MovieVO> movieVOS = CopyProperties.copyProperties(list, MovieVO.class);
		return Result.success(movieVOS);
	}


}




